/**
 * @description form表单的校验规则
 */
import isMobilePhone from 'validator/lib/isMobilePhone'
// import isIdentityCard from 'validator/lib/isIdentityCard'
import isEmail from 'validator/lib/isEmail'

/**
 * @description 多个字段翻译拼接的段落
 */
export const zhAndEnShowMsg = function () {
  const arr = Array.prototype.slice.call(arguments)
  return localStorage.getItem('language') === 'en'
    ? arr.join('\t')
    : arr.join('')
}

/**
 * 公共文本输入框的校验
 * 非空限制
 * @param {*} label
 * @returns Object
 */
export const commonInputText = (label = '', required = true) => {
  return [
    {
      required,
      message: () => zhAndEnShowMsg(g_t('public.pleaseInput'), label),
      trigger: ['blur', 'change']
    }
  ]
}

// 通用单行文本输入框的校验
export const singleLineText = (label = '') => {
  return [
    ...commonInputText(label),
    {
      pattern: /^[\u4e00-\u9fa5a-zA-Z0-9-_]{1,128}$/,
      message: () => g_t('rule.chineseLetterNumTips', { max: 128 }),
      trigger: ['blur', 'change']
    }
  ]
}

// 多行文本输入框的校验
export const multilineText = (label = '') => {
  return [
    ...commonInputText(label),
    {
      pattern: /^.{1,128}$/,
      message: () => g_t('rule.charLimitTips', { max: 128 }),
      trigger: ['blur', 'change']
    }
  ]
}

// 下拉框的校验
export const selectRule = (label = '') => {
  return [
    {
      required: true,
      message: () => zhAndEnShowMsg(g_t('public.pleaseSelect'), label),
      trigger: ['blur', 'change']
    }
  ]
}

// 上传文件的校验
export const uploadFileRule = (label = '') => {
  return [
    {
      required: true,
      message: () => zhAndEnShowMsg(g_t('public.pleaseUpload'), label),
      trigger: 'change'
    }
  ]
}

// 创建数字类型验证规则工厂
const createNumberRule = (label, errorMsg, required = true) => [
  ...commonInputText(label, required),
  {
    pattern: /^[1-9]\d*$/,
    message: () => errorMsg,
    trigger: ['blur', 'change']
  }
]

// 创建数字或小数点类型验证规则工厂
const createDecimalRule = (label, errorMsg, required = true) => [
  ...commonInputText(label, required),
  {
    // 只能输入正整数或者小数，不能以0开头的正整数，可以是一个0开头的小数，不能只输入一个0，不能只输入空字符串或多个空字符串
    // pattern: /^(0|[1-9]\d*)(\.\d+)?$/,
    pattern:
      /^(?!0$)(?!0\d)(?!0\.0$)(?!\s*$)([1-9]\d*(\.\d+)?|0\.\d*[1-9]\d*)$/,
    message: () => errorMsg,
    trigger: ['blur', 'change']
  }
]

// 创建零或数字或小数点类型验证规则工厂
const createDecimalRule2 = (label, errorMsg, required = true) => [
  ...commonInputText(label, required),
  {
    // 只能输入正整数或者小数，不能以0开头的正整数，可以是一个0开头的小数，可以输入一个0，不能只输入空字符串或多个空字符串
    pattern: /^(0|[1-9]\d*)(\.\d*[1-9]\d*|(\.0+)?)$/,
    message: () => errorMsg,
    trigger: ['blur', 'change']
  }
]

// 联系电话
export const phone = ({
  label = g_t('public.phone'),
  required = true
} = {}) => {
  return [
    ...commonInputText(label, required),
    {
      required,
      validator(rule, value, callback) {
        // 没填不需要校验，填了需校验
        if (!rule.required && !value.length) {
          callback()
        } else if (isMobilePhone(value, 'zh-CN')) {
          callback()
        } else {
          callback(new Error(g_t('rule.validatePhone')))
        }
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 用户名
export const userName = [
  ...commonInputText(g_t('public.userName')),
  {
    pattern: /^\w{1,30}$/,
    message: () => g_t('rule.letterNumTips', { max: 30 }),
    trigger: ['blur', 'change']
  }
]

// 邮箱
export const email = ({
  label = g_t('public.email'),
  required = true
} = {}) => {
  return [
    ...commonInputText(label, required),
    {
      required,
      validator(rule, value, callback) {
        // 没填不需要校验，填了需校验
        if (!rule.required && !value.length) {
          callback()
        } else if (isEmail(value)) {
          callback()
        } else {
          callback(new Error(g_t('rule.validateEmail')))
        }
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 密码
export const password = [
  ...commonInputText(g_t('public.password')),
  {
    validator(rule, value, callback) {
      // 须包含字母、数字，可以有特殊字符
      const pwdRegex =
        /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d!@#$%^&*()_+\-=\[\]{};:'",./<>?]{8,30}$/
      if (pwdRegex.test(value)) {
        callback()
      } else {
        callback(new Error(g_t('rule.passwordRequire', { min: 8, max: 30 })))
      }
    },
    trigger: ['blur', 'change']
  }
]

// 确认密码
export const confirmPassword = (formData, key) => {
  return [
    ...commonInputText(g_t('public.confirmPassword')),
    {
      validator(rule, value, callback) {
        if (formData[key] === value) {
          callback()
        } else {
          callback(new Error(g_t('rule.confirmPassword')))
        }
      },
      trigger: ['blur', 'change']
    }
  ]
}

// 翻倒次数
export const turnCount = [
  ...commonInputText('翻倒次数'),
  {
    pattern: /^([1-3])$/,
    message: () => '翻倒次数必须在1到3次之间',
    trigger: ['blur', 'change']
  }
]
